﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroConvenio : Form
    {
        private string operacao = "";

        public FormCadastroConvenio()
        {
            InitializeComponent();

        }

        private void FormCadastroConvenio_Load(object sender, EventArgs e)
        {
            //Popular a lista de convenios cadastrados
            List<MensalistaVO> listMensalistaVOs = ModelBO.getMensalistas(true);
            List<ClienteVO> listClienteVOs = ModelBO.getClientes(true);
            // remover os clientes mensalistas da lista
            foreach(MensalistaVO mensalistaVO in listMensalistaVOs){
                ClienteVO clienteVO = listClienteVOs.Find(c=> c.idCliente==mensalistaVO.idCliente);
                if (clienteVO!=null)
                    listClienteVOs.Remove(clienteVO);
            }

            this.dataGridViewConvenios.DataSource = ModelBO.getConvenios();
            this.comboBoxCliente.DataSource = listClienteVOs;
            this.comboBoxTabelaConvenio.DataSource = ModelBO.getTabelaConvenios(null, DateTime.Now, DateTime.Now);

             setupButtonsControls();
        }

        private void dataGridViewConvenio_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            this.operacao = "excluir";

            List<ConvenioVO> convenioList = (List<ConvenioVO>)this.dataGridViewConvenios.DataSource;
            ConvenioVO convenio = convenioList[this.dataGridViewConvenios.CurrentRow.Index];
            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão do convenio " + convenio.clienteVO.nome, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delConvenio(convenio);

                int nRow = this.dataGridViewConvenios.CurrentRow.Index;
                this.dataGridViewConvenios.DataSource = ModelBO.getConvenios();
                if (this.dataGridViewConvenios.Rows.Count > 0)
                {
                    dataGridViewConvenios.CurrentCell = dataGridViewConvenios.Rows[Math.Min(nRow, this.dataGridViewConvenios.Rows.Count - 1)].Cells[0];
                    dataGridViewConvenios.Rows[Math.Min(nRow, this.dataGridViewConvenios.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.numericUpDownDiaPgto.Value == 0)
                erros = erros + "Dia para pagamento deve ser informado.\r\n";

            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ConvenioVO convenio = null;

            if (this.operacao.Equals("novo"))
            {

                convenio = ConvenioVO.CreateConvenioVO();
                convenio.ativo = this.checkBoxAtivo.Checked;
                convenio.diaPgto = (int)this.numericUpDownDiaPgto.Value;
                convenio.vagas = (int)this.numericUpDownVagas.Value;
                convenio.idTabelaConvenio = (int)this.comboBoxTabelaConvenio.SelectedValue;
                convenio.idCliente = (int)this.comboBoxCliente.SelectedValue;
                convenio.situacao = this.textBoxSituacao.Text;

                convenio.idConvenio = (int)ModelBO.newConvenio(convenio);
            }
            if (this.operacao.Equals("editar"))
            {
                List<ConvenioVO> convenioList = (List<ConvenioVO>)this.dataGridViewConvenios.DataSource;
                convenio = convenioList[this.dataGridViewConvenios.CurrentRow.Index];

                convenio.idConvenio = int.Parse(this.textBoxId.Text);
                convenio.ativo = this.checkBoxAtivo.Checked;
                convenio.diaPgto = (int)this.numericUpDownDiaPgto.Value;
                convenio.vagas = (int)this.numericUpDownVagas.Value;
                convenio.idCliente = (int)this.comboBoxCliente.SelectedValue;
                convenio.idTabelaConvenio = (int)this.comboBoxTabelaConvenio.SelectedValue;
                convenio.situacao = this.textBoxSituacao.Text;

                ModelBO.setConvenio(convenio);

            }

            this.operacao = "salvar";

            List<ConvenioVO> listConvenioVOs = ModelBO.getConvenios();
            dataGridViewConvenios.DataSource = listConvenioVOs;
            int nRow = listConvenioVOs.FindIndex(a => a.idConvenio == convenio.idConvenio);
            dataGridViewConvenios.CurrentCell = dataGridViewConvenios.Rows[nRow].Cells[0];
            dataGridViewConvenios.Rows[nRow].Selected = true;

            setupButtonsControls();
        }

        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewConvenios.Rows.Count > 0 && this.dataGridViewConvenios.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.numericUpDownDiaPgto.Enabled = novo || edit;
            this.numericUpDownVagas.Enabled = novo || edit;
            this.comboBoxCliente.Enabled = novo || edit;
            this.comboBoxTabelaConvenio.Enabled = novo || edit;
            this.checkBoxAtivo.Enabled = novo || edit;

            this.dataGridViewConvenios.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxSituacao.Text = "Em aberto";
                this.numericUpDownDiaPgto.Value = 1;
                this.checkBoxAtivo.Checked = false;

            }
            else {
                List<ConvenioVO> convenioList = (List<ConvenioVO>)this.dataGridViewConvenios.DataSource;
                ConvenioVO convenio = convenioList[this.dataGridViewConvenios.CurrentRow.Index];
                this.textBoxId.Text = convenio.idConvenio.ToString();
                this.textBoxSituacao.Text = convenio.situacao;
                this.checkBoxAtivo.Checked = convenio.ativo;
                this.comboBoxCliente.SelectedValue = convenio.idCliente;
                this.comboBoxTabelaConvenio.SelectedValue = convenio.idTabelaConvenio;
                this.numericUpDownDiaPgto.Value = convenio.diaPgto;
                this.numericUpDownVagas.Value = convenio.vagas;

            }
            if (novo || edit){
                this.numericUpDownDiaPgto.Focus();
            }
        }

        private void dataGridViewConvenios_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            foreach (DataGridViewRow dgvr in dgv.Rows)
            {
                ConvenioVO cvo = (ConvenioVO)dgvr.DataBoundItem;
                dgvr.Cells["cliente"].Value = cvo.clienteVO.nome;
                dgvr.Cells["tabelaConvenio"].Value = cvo.tabelaConvenioVO.tabelaConvenio;
            }

        }

     }
}
